package com.situ.mall.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.common.util.PageResult;
import com.situ.mall.mapper.CartMapper;
import com.situ.mall.mapper.OrderItemMapper;
import com.situ.mall.mapper.OrderMapper;
import com.situ.mall.mapper.ProductMapper;
import com.situ.mall.pojo.entity.Order;
import com.situ.mall.pojo.entity.OrderItem;
import com.situ.mall.pojo.entity.Product;
import com.situ.mall.pojo.query.CartQuery;
import com.situ.mall.pojo.query.OrderQuery;
import com.situ.mall.pojo.vo.CartVO;
import com.situ.mall.pojo.vo.OrderVO;
import com.situ.mall.pojo.vo.ProductVO;
import com.situ.mall.service.IOrderService;
import com.situ.mall.util.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private SnowFlake snowFlake;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public void add(Order order) {
        //使用雪花算法生成order的主键
        long orderNo = snowFlake.nextId();
        order.setOrderNo(orderNo);
        //整个订单总金额
        BigDecimal payment = BigDecimal.valueOf(0.0);
        //查找这个用户购物车里面勾选的要结算的商品
        CartQuery cartQuery = new CartQuery();
        cartQuery.setUserId(order.getUserId());
        cartQuery.setChecked(1);
        List<CartVO> cartVOList = cartMapper.list(cartQuery);
        for (CartVO cartVO : cartVOList) {
            OrderItem orderItem = new OrderItem();
            orderItem.setOrderNo(orderNo);
            orderItem.setUserId(order.getUserId());
            orderItem.setProductId(cartVO.getProductId());
            orderItem.setProductName(cartVO.getProductName());
            orderItem.setProductImage(cartVO.getProductImage());
            orderItem.setCurrentUnitPrice(cartVO.getPrice());
            orderItem.setQuantity(cartVO.getQuantity());
            //orderItem.setTotalPrice(cartVO.getQuantity() * cartVO.getProductPrice());

            //计算当前商品的：价格*数量
            BigDecimal productPrice = cartVO.getPrice();
            BigDecimal quantity = BigDecimal.valueOf(cartVO.getQuantity());
            BigDecimal totalPrice = productPrice.multiply(quantity);
            orderItem.setTotalPrice(totalPrice);
            payment = payment.add(totalPrice);

            orderItemMapper.insertSelective(orderItem);

            //生成订单之后应该把购物车里面的商品删除+减少库存
            cartMapper.deleteByPrimaryKey(cartVO.getId());
            productMapper.reduceStock(cartVO.getProductId(), cartVO.getQuantity());
            productMapper.updateSales(cartVO.getProductId(), cartVO.getQuantity());

        }
        BigDecimal discount = order.getDiscount();
        Integer postage = order.getPostage();
        BigDecimal payment1 = payment.add(discount);
        BigDecimal payment2 = payment1.add(BigDecimal.valueOf(postage));
        order.setPayment(payment2);
        orderMapper.insertSelective(order);
    }

    @Override
    public List<OrderVO> list(OrderQuery orderQuery) {
        return orderMapper.list(orderQuery);
    }

    @Override
    public PageResult selectByPage(OrderQuery orderQuery) {
        //使用PageHelper开启分页
        PageHelper.startPage(orderQuery.getPage(), orderQuery.getLimit());
        //开启分页之后 LIMIT offset,pageSize 这个代码PageHelper帮我们做了
        List<Order> list = orderMapper.selectByPage(orderQuery);
        //查找总数量PageHelper会自动帮我们完成
        //int totalCount = userMapper.selectTotalCount(userQuery);
        PageInfo pageInfo = new PageInfo(list);
        int totalCount = (int) pageInfo.getTotal();

        return PageResult.ok(totalCount, list);
    }

    @Override
    public List<OrderItem> orderItemByOrderNo(Long orderNo) {
        return orderMapper.orderItemByOrderNo(orderNo);
    }

    @Override
    public void cancelOrder(Order order) {
        orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public Order orderByOrderNo(Long orderNo) {
        return orderMapper.selectByPrimaryKey(orderNo);
    }

    @Override
    public void deleteOrder(Order order) {
        orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public void update(Order order) {
        orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public void payOrderSuccess(Order order) {
        orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public Integer checkUserOrder(Integer userId) {
        return orderMapper.checkUserOrder(userId);
    }

    @Override
    public void buyNow(Order order, OrderItem orderItem) {
        //使用雪花算法生成order的主键
        long orderNo = snowFlake.nextId();
        order.setOrderNo(orderNo);
        //整个订单总金额
        BigDecimal payment = BigDecimal.valueOf(0.0);
        //查找这个用户购物车里面勾选的要结算的商品
        Product product = productMapper.selectByPrimaryKey(orderItem.getProductId());
        BigDecimal productPrice = product.getPrice();
        BigDecimal productQuantity = BigDecimal.valueOf(orderItem.getQuantity());
        BigDecimal totalPrice = productPrice.multiply(productQuantity);
        orderItem.setOrderNo(orderNo);
        orderItem.setUserId(order.getUserId());
        orderItem.setProductName(product.getName());
        orderItem.setProductImage(product.getMainImage());
        orderItem.setCurrentUnitPrice(product.getPrice());
        orderItem.setTotalPrice(totalPrice);
        payment = payment.add(totalPrice);
        orderItemMapper.insertSelective(orderItem);
        productMapper.reduceStock(orderItem.getProductId(), orderItem.getQuantity());
        productMapper.updateSales(orderItem.getProductId(), orderItem.getQuantity());
        BigDecimal discount = order.getDiscount();
        Integer postage = order.getPostage();
        BigDecimal payment1 = payment.add(discount);
        BigDecimal payment2 = payment1.add(BigDecimal.valueOf(postage));
        order.setPayment(payment2);
        orderMapper.insertSelective(order);
    }

//    @Override
//    public PageResult list(OrderQuery orderQuery) {
//        //使用PageHelper开启分页
//        PageHelper.startPage(orderQuery.getPage(), orderQuery.getLimit());
//        //开启分页之后 LIMIT offset,pageSize 这个代码PageHelper帮我们做了
//        List<OrderVO> list = orderMapper.list(orderQuery);
//        //查找总数量PageHelper会自动帮我们完成
//        //int totalCount = userMapper.selectTotalCount(userQuery);
//        PageInfo pageInfo = new PageInfo(list);
//        int totalCount = (int) pageInfo.getTotal();
//
//        return PageResult.ok(totalCount, list);
//        //return orderMapper.list(orderQuery);
//    }
}
